//---------------Sonic CD Swing Platform Script---------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
#alias Object.Value0	:	Object.Angle
#alias Object.Value1	:	Object.XDrawPos
#alias Object.Value2	:	Object.YDrawPos
#alias Object.Value3	:	Object.XVelocity
#alias Object.Value4	:	Object.YVelocity
#alias Object.Value5	:	Object.SwingAngle
#alias Object[24].PropertyValue	:	HUD.CurrentTimePeriod


#alias Object.PropertyValue	:	Object.ChainLength


sub ObjectMain
	Object.SwingAngle += 2
	Object.SwingAngle &= 511

	Sin(Object.Angle, Object.SwingAngle)
	Object.Angle >>= 2
	Object.Angle  += 128

	TempValue1 = Object.PropertyValue
	TempValue1++
	TempValue1 <<= 4

	Cos(Object.XVelocity, Object.Angle)
	Object.XVelocity  *= TempValue1
	Object.XVelocity <<= 7
	Object.XVelocity  += Object.XPos
	Object.XVelocity  &= 0xFFFF0000 // Truncate the value
	Object.XVelocity  -= Object.XDrawPos

	Sin(Object.YVelocity, Object.Angle)
	Object.YVelocity  *= TempValue1
	Object.YVelocity <<= 7
	Object.YVelocity  += Object.YPos
	Object.YVelocity  &= 0xFFFF0000 // Truncate the value
	Object.YVelocity  -= Object.YDrawPos
end sub


sub ObjectPlayerInteraction
	TempValue0 = Object.XPos
	TempValue1 = Object.YPos

	Object.XPos = Object.XDrawPos
	Object.YPos = Object.YDrawPos

	PlayerObjectCollision(C_PLATFORM, -24, -6, 24, 8)
	if CheckResult == true
		Player.XPos += Object.XVelocity
		Player.YPos += Object.YVelocity
	end if

	Object.XPos = TempValue0
	Object.YPos = TempValue1
end sub


sub ObjectDraw
	DrawSprite(0)
	TempValue0 = 0
	TempValue1 = 16
	while TempValue0 < Object.PropertyValue
		Cos(TempValue2, Object.Angle)
		TempValue2  *= TempValue1
		TempValue2 <<= 7
		TempValue2  += Object.XPos

		Sin(TempValue3, Object.Angle)
		TempValue3  *= TempValue1
		TempValue3 <<= 7
		TempValue3  += Object.YPos

		DrawSpriteXY(1, TempValue2, TempValue3)

		TempValue0++

		TempValue1 += 16
	loop

	Cos(Object.XDrawPos, Object.Angle)
	Object.XDrawPos  *= TempValue1
	Object.XDrawPos <<= 7
	Object.XDrawPos  += Object.XPos
	Object.XDrawPos  &= 0xFFFF0000 // Truncate the value

	Sin(Object.YDrawPos, Object.Angle)
	Object.YDrawPos  *= TempValue1
	Object.YDrawPos <<= 7
	Object.YDrawPos  += Object.YPos
	Object.YDrawPos  &= 0xFFFF0000 // Truncate the value

	DrawSpriteXY(2, Object.XDrawPos, Object.YDrawPos)
end sub


sub ObjectStartup
	LoadSpriteSheet("R2/Objects2.gif")

	TempValue0 = HUD.CurrentTimePeriod
	TempValue1 = 101
	TempValue0 *= 17
	TempValue1 += TempValue0 
	
	SpriteFrame(-8, -8, 16, 16, 230, 52)		// #0 - Connection Joint
	SpriteFrame(-8, -8, 16, 16, 230, 69)		// #1 - Chain
	SpriteFrame(-24, -8, 48, 16, 133, TempValue1)		// #2 - Platform
end sub


// ========================
// Editor Subs
// ========================

sub RSDKDraw
	DrawSprite(0)

	TempValue0 = 0
	TempValue1 = 16
	while TempValue0 < Object.PropertyValue
		Cos(TempValue2, Object.Angle)
		TempValue2  *= TempValue1
		TempValue2 <<= 7
		TempValue2  += Object.XPos

		Sin(TempValue3, Object.Angle)
		TempValue3  *= TempValue1
		TempValue3 <<= 7
		TempValue3  += Object.YPos

		DrawSpriteXY(1, TempValue2, TempValue3)

		TempValue0++

		TempValue1 += 16
	loop

	Cos(Object.XDrawPos, Object.Angle)
	Object.XDrawPos  *= TempValue1
	Object.XDrawPos <<= 7
	Object.XDrawPos  += Object.XPos
	Object.XDrawPos  &= 0xFFFF0000 // Truncate the value

	Sin(Object.YDrawPos, Object.Angle)
	Object.YDrawPos  *= TempValue1
	Object.YDrawPos <<= 7
	Object.YDrawPos  += Object.YPos
	Object.YDrawPos  &= 0xFFFF0000 // Truncate the value

	DrawSpriteXY(2, Object.XDrawPos, Object.YDrawPos)
	
	if editor.showGizmos == true
		editor.drawingOverlay = true
	
		// Draw an arrow to link it to its activator button
		TempValue0 = object.ypos
		TempValue0 -= 0x1000000
		//DrawArrow(object.xpos, object.ypos, object.xpos, TempValue0, 255, 255, 255)
		
		editor.drawingOverlay = false
	end if
end sub


sub RSDKLoad
	LoadSpriteSheet("R2/Objects2.gif")

	TempValue0 = HUD.CurrentTimePeriod
	TempValue1 = 101
	TempValue0 *= 17
	TempValue1 += TempValue0 
	
	SpriteFrame(-8, -8, 16, 16, 230, 52)		// #0 - Connection Joint
	SpriteFrame(-8, -8, 16, 16, 230, 69)		// #1 - Chain
	SpriteFrame(-24, -8, 48, 16, 133, 101)		// #2 - Platform
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "length")
end sub